export function parse(msg: string) {
  const parsed = JSON.parse(msg) as WechatRequest;

  const body = {
    query: '',
    response_mode: 'streaming',
    user: parsed.from.userid,
    inputs: {
      accessToken: 'accessToken',
      files: [] as any[],
    },
  };

  switch (parsed.msgtype) {
    case 'text':
      {
        body.query = parsed.text.content;
      }
      break;
    case 'image':
      {
        body.inputs.files = [
          {
            type: 'image',
            transfer_method: 'remote_url',
            url: parsed.image.url,
          },
        ];
      }
      break;
    case 'mixed': {
      body.query =
        parsed.mixed.msg_item.find((x) => x.msgtype === 'text')?.text.content ??
        '';

      const image = parsed.mixed.msg_item.find((x) => x.msgtype === 'image')
        ?.image.url;

      if (image) {
        body.inputs.files = [
          {
            type: 'image',
            transfer_method: 'remote_url',
            url: image,
          },
        ];
      }
      break;
    }
  }

  return {
    parsed,
    body,
  };
}
